<template>
    <div class="addadmin" style="width: 1280px;">
        <div class="admin_header">
            <div class="administrator_header">
                <div class="ax_default box_2" id="u9509">
                    <div id="u9509_div"></div>
                </div>
                <span class="header_words" v-show="type">新增信息公告</span>
                <span class="header_words" v-show="!type">更新信息公告</span>
            </div>

        </div>
        <div class="add_form">
            <div class="form_header">
                <span class="form_admin" v-show="type">编辑文章信息</span>
                <span class="form_admin" v-show="!type">更新文章信息</span>
            </div>
            <div class="form_news">
                <el-form :model="ruleForm" :rules="rules" label-position="left" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="标题" prop="newsTitle">
                        <el-input v-model="ruleForm.newsTitle" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="上传人" prop="name">
                        <el-input v-model="ruleForm.name" placeholder="请输入内容"></el-input>
                    </el-form-item>

                    <el-form-item label="内容" prop="content">
                        <div class="form_content" style="width: 977px;border: 1px solid #ccc;">
                            <div class="content_header">
                                <div class="content_img"><img src="../../../img/systemsettings/u8970.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8971.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8972.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8958.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8959.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8960.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8961.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8962.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8963.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8964.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8965.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8972.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8967.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8975.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8967.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8972.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8966.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8969.svg" alt=""></div>
                                <div class="content_img"><img src="../../../img/systemsettings/u8968.svg" alt=""></div>
                            </div>
                            <div class="content_input">
                                <input type="text" class="content-inp" v-model="ruleForm.newsText" placeholder="请输入内容"
                                    style="border: none;outline: none;">
                            </div>
                            <div class="content_bottom"></div>
                        </div>
                    </el-form-item>



                    <el-form-item label="状态" prop="delivery">
                        <el-switch v-model="ruleForm.delivery"></el-switch>
                    </el-form-item>
                    <el-form-item label="上传时间">
                        <div class="block">
                            <el-date-picker v-model="value1" type="date" placeholder="选择日期">
                            </el-date-picker>
                        </div>
                    </el-form-item>
                    <el-form-item class="form_btn">
                        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>

                        <el-button type="primary" plain @click="resetForm()">取消</el-button>
                    </el-form-item>

                </el-form>

            </div>

        </div>

    </div>
</template>
<script>
import createDate from '@/utils/createDate'
import { nanoid } from 'nanoid'
import { addUser, updataUserData } from '@/api/Informationments'
export default {
    name: 'Addinformation',
    data() {
        return {
            ruleForm: {
                newsTitle: '',
                name: '',
                newsText: '',
                newsText: ''
            },
            type: true,
            rules: {
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' },

                ],
                // uploaded: [
                //     { required: true, message: '请输入名字', trigger: 'blur' }
                // ],
                newsText: [
                    { required: true, message: '请输入内容', trigger: 'blur' }
                ],


            },

            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            value1: '',
        }
    },
    methods: {
        isAdd() {
            if (this.$route.params.type == 'updata') {
                this.type = false
                let _obj = this.$route.params._userObj

                this.ruleForm.newsTitle = _obj.newsTitle
                this.ruleForm.name = _obj.name
                this.ruleForm.newsText = _obj.newsText
            } else {
                this.type = true
            }
        },

        async submitForm() {
            //判断用户
            if (this.type) {
                if (
                    this.ruleForm.newsTitle.trim() != '' &&
                    this.ruleForm.name.trim() != ''
                ) {
                    let _obj = {
                        id: nanoid(),
                        newsTitle: this.ruleForm.newsTitle,
                        name: this.ruleForm.name,
                        newsText: this.ruleForm.newsText,
                        // addDate: nowTime,
                        isChecked: false,
                        isdelete: 0
                    }
                    // 添加用户
                    await addUser(_obj)
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.$router.push('/informent')
                } else {
                    this.$message({
                        message: '请不要输入空内容',
                        type: 'error'
                    })
                }
            } else {
                if (this.ruleForm.newsTitle.trim() != '' &&
                    this.ruleForm.name.trim() != '') {
                    let time = new Date()
                    let nowTime = createDate(time.toLocaleString('en-US', { hour12: false }).split(" "))
                    this.ruleForm.addDate = nowTime

                    await updataUserData(this.$route.params._userObj.id, this.ruleForm)

                    this.$message({
                        message: '更新成功',
                        type: 'success'
                    })
                    this.$router.push('/Informent')
                } else {
                    this.$message({
                        message: '请不要输入空内容',
                        type: 'error'
                    })
                }

            }
        },
        //取消按钮
        resetForm() {
            this.$router.go(-1);
        }

    },

    mounted() {
        //判断是添加用户，还是更新用户
        this.isAdd()
        // console.log(this.type);
    }
}
</script>
<style lang="scss" scoped>
.addadmin {
    margin: 0 auto;
}

.admin_header {
    display: flex;
    justify-content: space-between;
}

.administrator_header {
    display: flex;
    align-items: center;
}

#u9509 {
    border-width: 0px;
    width: 24px;
    height: 8px;
    display: flex;
    font-size: 16px;
    color: #006EFF;

}

#u9509_div {
    border-width: 0px;
    width: 24px;
    height: 8px;
    background: inherit;
    background-color: rgba(0, 110, 255, 1);
    border: none;
    border-radius: 4px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    font-size: 16px;
    color: #006EFF;
}

.header_words {
    margin-left: 20px;
    font-size: 24px;
}

.add_form {
    box-shadow: 0 0 20px #ccc;
    border-radius: 10px;
    margin-top: 20px;
    background-color: #fff;
}

.demo-ruleForm {
    width: 420px;
}

.form_header {
    border-radius: 10px 10px 0 0;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    background-color: rgba(242, 242, 242, 1);
    padding-left: 20px;
}

.form_admin {
    display: inline-block;
    color: #006EFF;
}

.form_news {
    padding: 20px 50px;
}


.el-switch__core {
    background: #fff;
}

.el-button {
    padding: 12px 50px;
    font-size: 14px;
}

.form_content {
    margin-bottom: 20px;
}

.content_bottom,
.content_header {
    height: 44px;
    background-color: rgba(242, 242, 242, 1);
}

.content_input {
    height: 266px;
    padding: 10px 20px;
}

.content_header {
    display: flex;
    align-items: center;
    padding-left: 20px;
}

.content_img {
    margin-right: 20px;
}

.content-inp {
    width: 100%;
    height: 270px;
}
</style>